<template>
	<view>
		<zmm-watermark :watermark="userStore.watermark" v-if="userStore.watermark"></zmm-watermark>
		<view class="content detail">
			<!--字典数据-->
			<view class="detail-item">
				<uni-section title="字典数据" type="line"></uni-section>
				<view class="detail-content formRef">
					<uni-forms ref="formRef" :model="formData" :rules="formRules" labelWidth="80px">
						<uni-forms-item label="字典类型" name="dictType" required>
					    <uni-easyinput disabled v-model="formData.dictType" placeholder="请输入字典类型" />
					  </uni-forms-item>
						<uni-forms-item label="数据标签" name="dictLabel" required>
						  <uni-easyinput v-model="formData.dictLabel" placeholder="请输入数据标签" />
						</uni-forms-item>
						<uni-forms-item label="数据键值" name="dictValue" required>
						  <uni-easyinput v-model="formData.dictValue" placeholder="请输入数据键值" />
						</uni-forms-item>
						<uni-forms-item label="显示排序" name="dictSort" required>
						  <uni-easyinput type="number" v-model="formData.dictSort" placeholder="请输入显示排序" />
						</uni-forms-item>
					  <uni-forms-item label="状态" name="status" required>
					    <view class="search-form-checkbox main-flex">
					    	<uni-data-checkbox v-model="formData.status" :localdata="systemDictStatusList" />
					    </view>
					  </uni-forms-item>
						<uni-forms-item label="备注" name="remark">
							<uni-easyinput type="textarea" v-model="formData.remark" placeholder="请输入备注"></uni-easyinput>
						</uni-forms-item>
					</uni-forms>
					<button type="primary" :disabled="isSubmit" :loading="isSubmit" @click="submit">提交</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { computed, onMounted, ref, watch, nextTick } from 'vue';
import { tab, auth, modal } from "@/plugins"
import { getTitle } from "@/utils/getter"
import { checkPermi, checkRole, checkOnlyPermi, checkOnlyRole } from "@/utils/permission"
import useUserStore from '@/store/modules/user'
import { timeFormat } from 'uview-plus'
import { getData, editData } from "@/api/system/dict/data"
import { systemDictStatusList } from '@/utils/config.js'

	const userStore = useUserStore()
	const dictCode = ref('')
	onLoad(async (option) => {
		if (option.dictCode) {
			uni.setNavigationBarTitle({
				title: '修改字典数据'
			})
			dictCode.value = option.dictCode
			getDetail()
		}
		if (option.dictType) {
			formData.value.dictType = option.dictType
		}
	})
	
	// 获取详情
	const info = ref({})
	const isLoading = ref(false)
	const getDetail = () => {
		isLoading.value = true
		uni.showLoading({
			title: '数据加载中...'
		})
		getData(dictCode.value).then(res => {
			info.value = res.data || {}
			formData.value = {
				dictType: res.data.dictType,
				dictLabel: res.data.dictLabel,
				dictValue: res.data.dictValue,
				dictSort: res.data.dictSort,
				dictCode: res.data.dictCode,
				remark: res.data.remark,
				status: Number(res.data.status)
			}
			isLoading.value = false
			uni.hideLoading()
	  }).catch(() => {
			isLoading.value = false
			uni.hideLoading()
		})
	}
	
	// form表单数据
	const formData = ref({
		dictType: '',
		dictLabel: '',
		dictValue: '',
		dictSort: '',
		dictCode: '',
		remark: '',
		status: 0
	})
	const formRef = ref(null)
	const formRules = ref({
		dictLabel: {
			rules: [{ required: true, errorMessage: '请输入数据标签' }]
		},
		dictValue: {
			rules: [{ required: true, errorMessage: '请输入数据键值' }]
		},
		dictSort: {
			rules: [{ required: true, errorMessage: '请输入显示排序' }]
		},
		status: {
			rules: [{ required: true, errorMessage: '请选择状态' }]
		}
	})
	
	const isSubmit = ref(false)
	// 提交
	const submit = () => {
		formRef.value.validate().then(formRes => {
			const params = { ...formRes }
			if (dictCode.value) {
				params.dictCode = dictCode.value
			}
			isSubmit.value = true
			editData(params).then(async (res) => {
				modal.showToast(res.msg)
				await uni.$u.sleep(1000)
				isSubmit.value = false
				clear()
				uni.$emit('refreshList');  // 触发刷新事件
				uni.navigateBack({
					delta: 1
				})
			})
		})
	}
	// 清空
	const clear = () => {
		formData.value = {
			dictType: '',
			dictLabel: '',
			dictValue: '',
			dictSort: '',
			dictCode: '',
			remark: '',
			status: 0
		}
	}
</script>

<style scoped lang="scss">
	
</style>
